//
//  QYBookAddInfoVC.m
//  QingYue
//
//  Created by Zheng on 2019/3/28.
//  Copyright © 2019 com.lanou.product_A. All rights reserved.
//

#import "QYBookAddInfoVC.h"
#import "QYBookAddInfoView.h"
#import "JYAddressPicker.h"

@interface QYBookAddInfoVC () <UITextFieldDelegate>

@property (nonatomic, strong) NSArray *selectedArr;
@property (nonatomic, copy) NSString *province;
@property (nonatomic, copy) NSString *city;
@property (nonatomic, copy) NSString *county;

@end

@implementation QYBookAddInfoVC {
    QYBookAddInfoView *infoView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"信息填写";

    self.view.backgroundColor = UIColorFromRGB(0xe3e3e3);;
    
    infoView = [[[NSBundle mainBundle] loadNibNamed:@"QYBookAddInfoView" owner:self options:nil] firstObject];
    infoView.cityTxf.delegate = self;
    [self.view addSubview:infoView];
    [infoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.right.mas_equalTo(-12);
        make.top.mas_equalTo(12);
        make.height.mas_equalTo(176);
    }];
    
    UIButton *commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [commitBtn setTitle:@"确定" forState:UIControlStateNormal];
    commitBtn.backgroundColor = MyGreenColor;
    [commitBtn addTarget:self action:@selector(commitBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:commitBtn];
    [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.offset(0);
        make.height.mas_equalTo(40);
    }];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    if (self.selectedArr) {
        
        NSArray *defaultValues = @[[self.selectedArr objectAtIndex:0][@"text"],[self.selectedArr objectAtIndex:1][@"text"],[self.selectedArr objectAtIndex:2][@"text"]];
        JYAddressPicker *addressPicker = [JYAddressPicker jy_showAt:self defaultShow:defaultValues];
        addressPicker.selectedItemBlock = ^(NSArray *addressArr) {
            
            _province = [addressArr objectAtIndex:0][@"text"];
            _city = [addressArr objectAtIndex:1][@"text"];
            _county = [addressArr objectAtIndex:2][@"text"];
            
            infoView.cityTxf.text = [NSString stringWithFormat:@"%@%@%@",_province,_city,_county];
            self.selectedArr = addressArr;
        };
    }else{
        JYAddressPicker *addressPicker = [JYAddressPicker jy_showAt:self];
        addressPicker.selectedItemBlock = ^(NSArray *addressArr) {
            
            _province = [addressArr objectAtIndex:0][@"text"];
            _city = [addressArr objectAtIndex:1][@"text"];
            _county = [addressArr objectAtIndex:2][@"text"];
            
            infoView.cityTxf.text = [NSString stringWithFormat:@"%@%@%@",_province,_city,_county];
            self.selectedArr = addressArr;
        };
        
    }

    return NO;
}

- (void)commitBtnAction {
    
    if (infoView.cityTxf.text.length == 0) {
        kShowNet(infoView.cityTxf.placeholder);
        kTimeAfter(0.5, ^{kHideNet;});
        return;
    }
    
    if (infoView.addressTxf.text.length == 0) {
        kShowNet(infoView.addressTxf.placeholder);
        kTimeAfter(0.5, ^{kHideNet;});
        return;
    }
    
    if (infoView.nameTxf.text.length == 0) {
        kShowNet(infoView.nameTxf.placeholder);
        kTimeAfter(0.5, ^{kHideNet;});
        return;
    }
    
    if (infoView.mobileTxf.text.length == 0) {
        kShowNet(infoView.mobileTxf.placeholder);
        kTimeAfter(0.5, ^{kHideNet;});
        return;
    }
    
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:[kUserDefaults objectForKey:@"username"] forKey:@"UserName"];
    [param setObject:[NSString stringWithFormat:@"%@%@",_province,_city] forKey:@"RecipientsAddress1"];
    [param setObject:infoView.addressTxf.text forKey:@"RecipientsAddress3"];
    [param setObject:_county forKey:@"RecipientsAddress2"];
    [param setObject:infoView.mobileTxf.text forKey:@"RecipientsTel"];
    [param setObject:infoView.nameTxf.text forKey:@"RecipientsName"];
    [param setObject:_model.Id forKey:@"BookID"];
    
    [[YCRequestDataService sharedManager] Post:@"AddOrderDetails" parameters:param success:^(id response) {
        if ([response[@"Code"] isEqualToString:@"成功"]) {
            kTimeAfter(1, ^{kShowNet(@"添加成功");});
            kTimeAfter(1.5, ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        } else {
            kTimeAfter(1, ^{kShowNet(response[@"Exception"]);});
        }
    } failure:^{
        kTimeAfter(1, ^{kShowNet(@"网络或服务器错误");});
    }];

    
    
}



@end
